package com.xs.tool.common.encryption;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * RSA加、解密算法工具类
 */
@Slf4j
public class RsaUtil {

	public static final String LOGIN_PUBKEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCsZDyZNj4PaR+lgIy45ZFO5xDXEIf+xsucJ9Hj/Mu87vJ7+OI/QJPmJwPMHWyRfycIsikvHVjuwh+6sFWsZlSFmaGzFK7BIephryj4/j+ZcX45f2KvraHAOB5XuwfwkjgmlOclr55K1dhNl8bSbuL2estMV7IpIKoic157G3A4IQIDAQAB";
	public static final String LOGIN_PRIKEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKxkPJk2Pg9pH6WAjLjlkU7nENcQh/7Gy5wn0eP8y7zu8nv44j9Ak+YnA8wdbJF/JwiyKS8dWO7CH7qwVaxmVIWZobMUrsEh6mGvKPj+P5lxfjl/Yq+tocA4Hle7B/CSOCaU5yWvnkrV2E2XxtJu4vZ6y0xXsikgqiJzXnsbcDghAgMBAAECgYAMcyzBL96phEEJ1c/T5pho510a+xGnP7oiq+mHmpEoMuWGOII2aTaFL5ohmpplo5YxFZTN10O98n0SIXv+eGFwKjtRJ3A7WeIjUB/rAV4SkfXBK1B10MTPiIAP/Pq8HAUWneI8/1G7No7aCLEZLBS55R5MSDEk647W8Ond4sHZ8QJBANp9BbCpDrRTozODNQv95bBE0GXoTvCfa6z/N7Zt9zS7wvElwmYsvhi9IvX5Ky4kQo9c9Vw7cbHwwdxeMQhuRI0CQQDJ/SwGUgboZZ8LIZ7hYHtWwsmykWmYrpXPrGCY311VaoFxn5bNirnjBL3wcMqCQFqS/HZTtnsIL4iFN2qMLf7lAkA8kMGeCg8Yb8LiybFPxK8CIOFADpirXRXlG85e1LbdOFs+q7/qwOZlRzDEzLBqlwSYZyVgzt2ifOHQa4/eDWsVAkBLpi8z2srNhSnQwHHB1SG8d82UvFNBJLnlAplXB+jr0azTQkL3jipadO8yOvwNVk7V2An1BlYD1dGamwujnqJlAkEAhuiHkG8zYZZOZrKUrVV3ENLs8DbHN/sftdcSokUZ0hwZolHGvxB85Ga6oNL/iOqim7/IyhXAQU62zczMB1GDzQ==";
	/**
	 * 加密算法AES
	 */
	private static final String KEY_ALGORITHM = "RSA";

	/**
	 * 算法名称/加密模式/数据填充方式 默认：RSA/ECB/PKCS1Padding
	 */
	private static final String ALGORITHMS = "RSA/ECB/PKCS1Padding";

	/**
	 * Map获取公钥的key
	 */
	private static final String PUBLIC_KEY = "publicKey";

	/**
	 * Map获取私钥的key
	 */
	private static final String PRIVATE_KEY = "privateKey";

	/**
	 * RSA最大加密明文大小
	 */
	private static final int MAX_ENCRYPT_BLOCK = 117;

	/**
	 * RSA最大解密密文大小
	 */
	private static final int MAX_DECRYPT_BLOCK = 128;

	/**
	 * RSA 位数 如果采用2048 上面最大加密和最大解密则须填写:  245 256
	 */
	private static final int INITIALIZE_LENGTH = 1024;

	/**
	 * 后端RSA的密钥对(公钥和私钥)Map，由静态代码块赋值
	 */
	private static Map<String, Object> genKeyPair = new LinkedHashMap<>();

	static {
		try {
			genKeyPair.putAll(genKeyPair());
		} catch (Exception e) {
			//输出到日志文件中
			log.error(e.getMessage());
		}
	}

	/**
	 * 生成密钥对(公钥和私钥)
	 */
	private static Map<String, Object> genKeyPair() throws Exception {
		KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
		keyPairGen.initialize(INITIALIZE_LENGTH);
		KeyPair keyPair = keyPairGen.generateKeyPair();
		RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
		RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
		Map<String, Object> keyMap = new HashMap<String, Object>(2);
		//公钥
		keyMap.put(PUBLIC_KEY, publicKey);
		//私钥
		keyMap.put(PRIVATE_KEY, privateKey);
		return keyMap;
	}

	/**
	 * 私钥解密
	 *
	 * @param encryptedData 已加密数据
	 * @param privateKey    私钥(BASE64编码)
	 */
	public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey)
		throws Exception {
		//base64格式的key字符串转Key对象
		Key privateK = KeyFactory.getInstance(KEY_ALGORITHM)
			.generatePrivate(new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey)));

		//设置加密、填充方式
        /*
            如需使用更多加密、填充方式，引入
            <dependency>
                <groupId>org.bouncycastle</groupId>
                <artifactId>bcprov-jdk16</artifactId>
                <version>1.46</version>
            </dependency>
            并改成
            Cipher cipher = Cipher.getInstance(ALGORITHMS ,new BouncyCastleProvider());
         */
		Cipher cipher = Cipher.getInstance(ALGORITHMS);
		cipher.init(Cipher.DECRYPT_MODE, privateK);

		//分段进行解密操作
		return encryptAndDecryptOfSubsection(encryptedData, cipher, MAX_DECRYPT_BLOCK);
	}

	/**
	 * 公钥加密
	 *
	 * @param data      源数据
	 * @param publicKey 公钥(BASE64编码)
	 */
	public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Exception {
		//base64格式的key字符串转Key对象
		Key publicK = KeyFactory.getInstance(KEY_ALGORITHM)
			.generatePublic(new X509EncodedKeySpec(Base64.decodeBase64(publicKey)));

		//设置加密、填充方式
        /*
            如需使用更多加密、填充方式，引入
            <dependency>
                <groupId>org.bouncycastle</groupId>
                <artifactId>bcprov-jdk16</artifactId>
                <version>1.46</version>
            </dependency>
            并改成
            Cipher cipher = Cipher.getInstance(ALGORITHMS ,new BouncyCastleProvider());
         */
		Cipher cipher = Cipher.getInstance(ALGORITHMS);
		cipher.init(Cipher.ENCRYPT_MODE, publicK);

		//分段进行加密操作
		return encryptAndDecryptOfSubsection(data, cipher, MAX_ENCRYPT_BLOCK);
	}

	/**
	 * 获取私钥
	 */
	public static String getPrivateKey() {
		Key key = (Key) genKeyPair.get(PRIVATE_KEY);
		return Base64.encodeBase64String(key.getEncoded());
	}

	/**
	 * 获取公钥
	 */
	public static String getPublicKey() {
		Key key = (Key) genKeyPair.get(PUBLIC_KEY);
		return Base64.encodeBase64String(key.getEncoded());
	}

	public static Map<String, String> getKes() {
		Key key = (Key) genKeyPair.get(PRIVATE_KEY);
		String privateKey = Base64.encodeBase64String(key.getEncoded());
		Key keyPub = (Key) genKeyPair.get(PUBLIC_KEY);
		String publicKey = Base64.encodeBase64String(keyPub.getEncoded());
		Map<String, String> keyMap = new HashMap<String, String>(2);
		//公钥
		keyMap.put(PUBLIC_KEY, publicKey);
		//私钥
		keyMap.put(PRIVATE_KEY, privateKey);
		return keyMap;
	}

	/**
	 * 分段进行加密、解密操作
	 */
	private static byte[] encryptAndDecryptOfSubsection(byte[] data, Cipher cipher,
		int encryptBlock) throws Exception {
		int inputLen = data.length;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		int offSet = 0;
		byte[] cache;
		int i = 0;
		// 对数据分段加密
		while (inputLen - offSet > 0) {
			if (inputLen - offSet > encryptBlock) {
				cache = cipher.doFinal(data, offSet, encryptBlock);
			} else {
				cache = cipher.doFinal(data, offSet, inputLen - offSet);
			}
			out.write(cache, 0, cache.length);
			i++;
			offSet = i * encryptBlock;
		}
		out.close();
		return out.toByteArray();
	}
}
